package admin_service

import (
	"shop/dao"
	"shop/entity"
	"shop/utils/result"
)

func GetGoodsCategoryList(shopId int) *result.Result {
	categoryList := make([]*entity.GoodsCategory, 0, 0)
	dao.GetDB().Table(entity.TableNameGoodsCategory).Where("shop_id", shopId).Order("display_order").Find(&categoryList)
	return result.Ok(categoryList)
}

func AddGoodsCategory(shopId int, goodsCategory *entity.GoodsCategory) *result.Result {
	goodsCategory.ShopId = shopId
	goodsCategory.ShowStatus = true
	rows := dao.GetTx().Model(goodsCategory).Create(goodsCategory).Commit().RowsAffected
	return result.Ok(rows)
}

func UpdateGoodsCategory(id, shopId int, goodsCategory *entity.GoodsCategoryFilter) *result.Result {
	rows := dao.GetTx().Model(goodsCategory).Where("id=? and shop_id=?", id, shopId).Updates(goodsCategory).Commit().RowsAffected
	return result.Ok(rows)
}

func DeleteGoodsCategory(id, shopId int) *result.Result {
	tx := dao.GetTx()
	tx.Table(entity.TableNameGoodsCategory).Where("id=? and shop_id=?", id, shopId).Delete(nil)
	tx.Table(entity.TableNameGoods).Where("goods_category_id", id).Delete(nil)
	tx.Commit()
	if tx.Error != nil {
		return result.Fail(tx.Error.Error())
	}
	return result.Ok(nil)
}
